import {lazyAMapApiLoaderInstance} from "vue-amap";

export function setting(self){
  return {
    zoom:17,
    center:[116.397428, 39.90923],
    events: {
      init (map) {
        self.lazyAMapApiLoaderInstance.load().then(() => {
          self.map = map;
          self.geocoder = new AMap.Geocoder({
            radius: 1000,
            extensions: "all"
          });
        });
      }
    },
    searchOption: {
      /*city: '上海',
      citylimit: true*/
    },
    plugin: [{
      pName: 'ToolBar',
      position:'LT',
      events: {
        init(instance) {
          console.log(instance);
        }
      }
    },{
      pName: 'MapType',
      defaultType: 0,
      events: {
        init(instance) {
          console.log(instance);
        }
      }
    },{
      pName: 'OverView',
      events: {
        init(instance) {
          console.log(instance);
        }
      }
    },{
      pName: 'Scale',
      events: {
        init(instance) {
          console.log(instance);
        }
      }
    }]
  };
}
export function regeocoder(self,pathPoint,funBack){
  self.geocoder.getAddress([pathPoint.longitude, pathPoint.latitude], function(status, result) {
    if (status === 'complete' && result.info === 'OK') {
      funBack(result.regeocode.formattedAddress);
    }
  });
}
export function addLine(self,path){
  AMapUI.load(['ui/misc/PathSimplifier', 'lib/$'], function(PathSimplifier, $) {
    if (!PathSimplifier.supportCanvas) {
      alert('当前环境不支持 Canvas！');
      return;
    }
    let pathSimplifierIns = self.pathSimplifierIns?self.pathSimplifierIns:new PathSimplifier({
      zIndex: 100,
      map: self.map, //所属的地图实例
      getPath: function(pathData, pathIndex) {
        if(pathIndex&&pathIndex>-1){
          return pathData.path[pathIndex];
        }
        return pathData.path;
      },
      getHoverTitle: function(pathData, pathIndex, pointIndex) {
        let point = self.linePoints[pointIndex];
        if(pointIndex>0){
          return `时间：${point.locationTime}<br/>速度：${point.speed}`;
        }
      },
      renderOptions: {
        renderAllPointsIfNumberBelow: 100, //绘制路线节点，如不需要可设置为-1
        eventSupport: true,
        pathLineStyle: {
          lineWidth: 2,
            fillStyle: null,
            strokeStyle: 'block',
            borderStyle: 'blue'
        }
      }
    });
    self.pathSimplifierIns = pathSimplifierIns;
    //设置数据
    pathSimplifierIns.setData([{
      name: '轨迹回放',
      path: path
    }]);
    //对第一条线路（即索引 0）创建一个巡航器
    self.firstPath = pathSimplifierIns.createPathNavigator(0, {
      loop: false, //循环播放
      speed: 100, //巡航速度，单位千米/小时
      pathNavigatorStyle: {
        width: 24,//图中小车宽度
        height: 48,//图中小车高度
        //使用图片
        content: PathSimplifier.Render.Canvas.getImageContent(require('@/assets/vehicle_map/image/truck.png')),
        strokeStyle: null,
        fillStyle: null,
        //经过路径的样式
        pathLinePassedStyle: {
          lineWidth: 4,
          strokeStyle: 'black',
          dirArrowStyle: {
            stepSpace: 15,
            strokeStyle: 'red'
          }
        }
      },
    });
    self.markers.push(new AMap.Marker({
      map: self.map,
      position: path[0],
      icon: new AMap.Icon({
        size: new AMap.Size(32, 32),
        image: require('@/assets/vehicle_map/image/line_begin.png'),
        imageSize: new AMap.Size(32, 32)
      }),
      offset: new AMap.Pixel(-16, -32),
      autoRotation: true,
      angle:0,
    }));
    self.markers.push(new AMap.Marker({
      map: self.map,
      position: path[path.length-1],
      icon: new AMap.Icon({
        size: new AMap.Size(32, 32),
        image: require('@/assets/vehicle_map/image/line_end.png'),
        imageSize: new AMap.Size(32, 32)
      }),
      offset: new AMap.Pixel(-16, -32),
      autoRotation: true,
      angle:0,
    }));
    self.firstPath.on('move', function (e) {
      let idx = this.getCursor().idx; //走到了第几个点
      let tail = this.getCursor().tail; //至下一个节点的比例位置
      let percent = Math.round(((idx+tail)/path.length) * 100);
      if(percent>self.sliderVal){
        self.sliderVal=percent;
      }
      if(idx>self.lineIndex){
        self.changePoint(idx);
      }
    });
    self.lineSpeed = 100;
  });
}
